<template>
	<view class="inviteCode" :style="{backgroundImage:'url('+codeImg+')'}" @tap="saveImg()">
	    <!-- #ifdef APP-PLUS -->
	    <view class="share_footer">
	    	<view class="share_box">
	    		<view class="share_box_span" @click="share">
	    			<image src="/static/images/share/weixin.png" class="share_icon" mode="widthFix"></image>
	    			<text class="share_box_text">分享微信好友</text>
	    		</view>
	    		<view class="share_box_span" @click="share">
	    			<image src="/static/images/share/pyq.png" class="share_icon" mode="widthFix"></image>
	    			<text class="share_box_text">朋友圈</text>
	    		</view>
	    		<view class="share_box_span" @click="saveCans(1)">
	    			<image src="/static/images/share/save.png" class="share_icon" mode="widthFix"></image>
	    			<text class="share_box_text">保存手机</text>
	    		</view>
	    	</view>

	    </view>
	    <!-- #endif -->
	</view>

</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				codeImg: '',
				username: '',
				shareId: uni.getStorageSync('account')
			};
		},
		onShow() {
			this.tui.haslogin();
			if (this.tui.isLogin()) {
				this.initData();
			}
		},
		computed: {
			...mapState(['account']),
		},
		methods: {
			initData() {
				let imgs = this.tui.getImgs();
				// if (imgs) {
				// 	this.codeImg = imgs;
				// } else {
					let token = this.tui.getToken();
					let datas = {
						token: token,
					};
					let result = this.tui.request('user', 'post', datas, 1000, 1, '', 'shareCode');
					Promise.resolve(result).then((value) => {
						
						if (value.code == 200) {
							
							this.codeImg = value.data.qr_code;
							this.tui.setImgs(this.codeImg);
						} else {
							this.tui.toast(value.message);
						}
					})
				// }
			},
			onShareAppMessage(res) {
				if (res.from === 'button') { // 来自页面内分享按钮
				}
				this.userinfo = this.tui.getUserInfoData();
				return {
					title: '来自' + this.userinfo.user_tel + '的分享码',
				}
			},
			share() {
				//#ifdef H5
				this.tui.toast("请用微信右上角分享");
				return;
				//#endif
				let tempRatio = 1.5;

				// #ifdef H5 || APP-PLUS
				tempRatio = 1;
				// #endif

				uni.shareWithSystem({
					provider: "weixin",
					// scene: "WXSceneSession",
					type: 2,
					imageUrl: this.codeImg,
					success: function(res) {
						uni.showToast({
							title: JSON.stringify(err),
							duration: 2000,
							icon: 'none'
						});
					},
					fail: function(err) {
				
						console.log("fail:" + JSON.stringify(err));
					}
				});

			},
			saveCans(e) { //保存海报
				//#ifdef H5
				this.tui.toast("请长按保存图片");
				return;
				//#endif
				//#ifdef APP-PLUS
				uni.showLoading({
					title: '保存海报中'
				});
				let tempRatio = 1.5;

				// #ifdef H5 || APP-PLUS

				tempRatio = 1;
				// #endif

				uni.saveImageToPhotosAlbum({
					filePath: this.codeImg,
					success: function(red) {
						uni.showToast({
							title: '保存相册成功'
						})
						that.imgsrc = this.codeImg
						if (e == 2) {

						}
					},
					fail(res) {
						if (res.errMsg == "saveImageToPhotosAlbum:fail auth deny") {
							uni.showModal({
								title: '您需要授权相册权限',
								success(res) {
									if (res.confirm) {
										uni.openSetting({
											success(res) {

											},
											fail(res) {
												console.log(res)
											}
										})
									}
								}
							})
						}
					}
				});
				//#endif
			},

		}
	}
</script>

<style lang="scss">
	page {
		height: 100vh;
	}

	.share_footer {
		width: 100%;
		position: fixed;
		bottom: 0rpx;
		height: 140rpx;
		z-index: 100000;
		background: #fff;

	}

	.inviteCode {
		// min-height: 100%;
		background-color: #eae6e3;
		background-repeat: no-repeat;
		background-size: 100%;

	}

	.share_box {
		text-align: center;
		margin: 20rpx 0;
		justify-content: space-between;
		font-size: 22rpx;
		line-height: 34rpx;
		color: #353535;
		display: flex;
	}

	.share_box_span {
		display: block;
		width: 33%;
	}

	.share_box_text {
		display: block;
		text-align: center;
	}

	.share_icon {
		width: 60rpx;
	}
</style>
